package com.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dao.staffDao;

/**
 * Servlet implementation class export2
 */
@WebServlet("/export2.xls")
public class export2 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public export2() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		response.setContentType("text/application;charset=GBK");
		staffDao dao = new staffDao();
		String flag = request.getParameter("flag");
		List<Map> list;
		if(flag.equals("weidaoqi")) {
			list = dao.getweidaoqi();
			String res = "";
			for(int i=0;i<list.size();i++) {
				Map m = list.get(i);
				res+=m.get("name")+"\t";
				res+=m.get("personid")+"\t";
				res+=m.get("sex")+"\t";
				res+=m.get("nation")+"\t";
				res+=m.get("face")+"\t";
				res+=m.get("department")+"\t";
				res+=m.get("education")+"\t";
				res+=m.get("contact")+"\t";
				res+=m.get("pid")+"\t";
				res+=m.get("kind")+"\t";
				res+=m.get("startdate")+"\t";
				res+=m.get("enddate")+"\t";
				res+=m.get("returndate")+"\t\n";
			}
			res = res.replaceAll("\\{", "");
			res = res.replaceAll("\\}", "");
			res = res.replaceAll("\r", "");
			System.out.println("%%%2"+res);
			response.getWriter().print(res);
			return;
		}
		else {
			list = dao.getweihuiguo();
			String res = "";
			for(int i=0;i<list.size();i++) {
				Map m = list.get(i);
				res+=m.get("name")+"\t";
				res+=m.get("personid")+"\t";
				res+=m.get("sex")+"\t";
				res+=m.get("nation")+"\t";
				res+=m.get("face")+"\t";
				res+=m.get("job")+"\t";
				res+=m.get("adl")+"\t";
				res+=m.get("department")+"\t";
				res+=m.get("education")+"\t";
				res+=m.get("contact")+"\t";
				res+=m.get("pid")+"\t";
				res+=m.get("kind")+"\t";
				res+=m.get("startdate")+"\t";
				res+=m.get("enddate")+"\t";
				res+=m.get("country")+"\t";
				res+=m.get("college")+"\t";
				res+=m.get("subject")+"\t";
				res+=m.get("outdate")+"\t";
				if((m.get("backdate")).equals("")) res+="未回\t";
				else res+=m.get("backdate")+"\t";
				res+=m.get("costby")+"\t";
				if((m.get("returndate")).equals("")) res+="尚未归还\t";
				else res+=m.get("returndate")+"\t";
				if((m.get("remake")).equals("")) res+="无\t\n";
				else res+=m.get("remake")+"\t\n";
			}
			res = res.replaceAll("\\{", "");
			res = res.replaceAll("\\}", "");
			res = res.replaceAll("\r", "");
			System.out.println("%%%2"+res);
			response.getWriter().print(res);
		}
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
